package com.lepu.app.fun.tool.cjsj.bean;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;
import com.app.utils.DateUtil;

public class BeanToolChanJian implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String TABLE_NAME = "ToolChanJian";
	
	public int id;
	public String uid; 
	public String date; 
	public int remindIndex; 
	public int remindId; 
	public int remindIs;
	public String remindTime; 
    public String note; //注释
    
    
    public static ArrayList<BeanToolChanJian> parseJsonData(String dataString) {
    	ArrayList<BeanToolChanJian> topicList = new ArrayList<BeanToolChanJian>();
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray dataArray = obj1.getJSONArray("ListInfo");

            for(int i=0; i<dataArray.length(); i++){
				JSONObject focusJson = (JSONObject) dataArray.get(i);
				BeanToolChanJian bean = new BeanToolChanJian();
				bean.id = focusJson.optInt("UPCID");
				bean.uid = focusJson.optString("UserID");
				bean.date = DateUtil.getYMDDate(focusJson.optString("CheckDate"));
				bean.note = focusJson.optString("CheckItems");
				bean.remindIs = focusJson.optInt("IsAlert");
				bean.remindIndex = focusJson.optInt("AlertBeforeDay");
				bean.remindTime = DateUtil.getHM2Time(focusJson.optString("AlertTime"));
				
				topicList.add(bean);
			}
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return topicList;
    }
    

    //添加
    public static boolean insert(BeanToolChanJian bean) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("uid", bean.uid);
        values.put("date", bean.date);
        values.put("remindIndex", bean.remindIndex);
        values.put("remindTime", bean.remindTime);
        values.put("note", bean.note);
        values.put("remindId", bean.remindId);
        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolChanJian> getDataByUid(String uid) {
    	ArrayList<BeanToolChanJian> provinces = new ArrayList<BeanToolChanJian>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "date", "remindIndex", "remindTime","note", "remindId"}, 
					"uid=?",new String[] {uid}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    // 修改数据
    public static boolean update(BeanToolChanJian bean) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        
        values.put("uid", bean.uid);
        values.put("date", bean.date);
        values.put("remindIndex", bean.remindIndex);
        values.put("remindTime", bean.remindTime);
        values.put("remindId", bean.remindId);
        values.put("note", bean.note);

        flag = dbHelp.update(TABLE_NAME, values, "uid=? and id=?", new String[]{bean.uid, String.valueOf(bean.id)});

        return flag;
    }
    
    
	//是否存在
	public static boolean isExist(String uid) {
		ArrayList<BeanToolChanJian> provinces = new ArrayList<BeanToolChanJian>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return false;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "time", "name", "number","note", "done"}, 
					"uid=?", new String[] { uid },null, null, null);
			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		if (provinces.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	//删除某一用户数据
    public static boolean deleteUserDate(String uid) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "uid=?",new String[]{uid});

        return flag;
    }
    
	//删除某一条数据
    public static boolean deleteItem(String uid, int id) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "uid=? and id=?",new String[]{uid,String.valueOf(id)});

        return flag;
    }
	
	private static void cursorParse(ArrayList<BeanToolChanJian> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanToolChanJian beanTarget = new BeanToolChanJian();
			beanTarget.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanTarget.uid = (cursor.getString(cursor.getColumnIndex("uid")));
			beanTarget.date = (cursor.getString(cursor.getColumnIndex("date")));
			beanTarget.remindIndex = (cursor.getInt(cursor.getColumnIndex("remindIndex")));
			beanTarget.remindId = (cursor.getInt(cursor.getColumnIndex("remindId")));
			beanTarget.remindTime = (cursor.getString(cursor.getColumnIndex("remindTime")));
			beanTarget.note = (cursor.getString(cursor.getColumnIndex("note")));
			msgArray.add(beanTarget);
		}
	}
	
}
